package com.loong.socket.model;

import android.util.Log;

import java.util.Arrays;

public class TzcFileInternet {
    private static final String TAG = TzcFileInternet.class.getSimpleName();
    private static int order = 1;
    private static final int _PROTOCOLTYPEPOS =    0;   //协议类型位置（50个字节序位置,第0字节表示高位，第1字节表示低位）
    private static final int _ACKXBITPOS =       13;   //_ACKBMPXYSIZE类型时13-16字节记录X图像显示像素大小
    private static final int _ACKYBITPOS =       17;   //_ACKBMPXYSIZE类型时13-16字节记录Y图像显示像素大小
    private static final int _DATABLOCKNOPOS =    8;   //包号位置 (4个字节表示)

    public static short getProtocolType(byte[] buffer) {
        Log.i(TAG, "buffer[0] = " + buffer[_PROTOCOLTYPEPOS] +" buffer[1] = " + buffer[_PROTOCOLTYPEPOS + 1]);
        short ret = (short) (buffer[_PROTOCOLTYPEPOS] << 8 | buffer[_PROTOCOLTYPEPOS + 1] & 0xff);
        return ret;
    }

    public static int getBlockNo(byte[] buffer) {
        Log.i(TAG, "buffer[8] = " + buffer[_DATABLOCKNOPOS] +" buffer[9] = " + buffer[_DATABLOCKNOPOS + 1]);
        Log.i(TAG, "buffer[10] = " + buffer[_DATABLOCKNOPOS + 2] +" buffer[11] = " + buffer[_DATABLOCKNOPOS + 3]);
        int ret = buffer[_DATABLOCKNOPOS] << 24 | buffer[_DATABLOCKNOPOS + 1] << 16 | buffer[_DATABLOCKNOPOS + 2] << 8 | buffer[_DATABLOCKNOPOS + 3] & 0xff;
        return ret;
    }

    public static int getBitmapWidth(byte[] buffer) {
        Log.i(TAG, "buffer[13] = " + buffer[_ACKXBITPOS] +" buffer[14] = " + buffer[_ACKXBITPOS + 1]);
        Log.i(TAG, "buffer[15] = " + buffer[_ACKXBITPOS + 2] +" buffer[16] = " + buffer[_ACKXBITPOS + 3]);
        int ret = buffer[_ACKXBITPOS] << 24 | buffer[_ACKXBITPOS + 1] << 16 | buffer[_ACKXBITPOS + 2] << 8 | buffer[_ACKXBITPOS + 3] & 0xff;
        return ret;
    }

    public static int getBitmapHeight(byte[] buffer) {
        Log.i(TAG, "buffer[17] = " + buffer[_ACKYBITPOS] +" buffer[18] = " + buffer[_ACKYBITPOS + 1]);
        Log.i(TAG, "buffer[19] = " + buffer[_ACKYBITPOS + 2] +" buffer[20] = " + buffer[_ACKYBITPOS + 3]);
        int ret = buffer[_ACKYBITPOS] << 24 | buffer[_ACKYBITPOS + 1] << 16 | buffer[_ACKYBITPOS + 2] << 8 | buffer[_ACKYBITPOS + 3] & 0xff;
        return ret;
    }

    public static void msgTypeInfo(byte[] buffer, short type, int level, byte mark, String mac) {
        buffer[0] = (byte) ((type >> 8) & 0xff);//协议类型
        buffer[1] = (byte) (type & 0xff);
        //  USHORT m_commi_bianhao;//数据编号
        // 	ULONG  m_data_size;    //传输文件的长度
        buffer[8] = (byte) ((level >> 24) & 0xff);    //包号
        buffer[9] = (byte) ((level >> 16) & 0xff);
        buffer[10] = (byte) ((level >> 8) & 0xff);
        buffer[11] = (byte) (level & 0xff);
        buffer[12] = mark;   //传输完成标志未完成/否则完成
        // 	BYTE   m_unin_info[14];//文件名/花版号/其它
        // 	USHORT m_file_num;     //文件个数
        // 	USHORT m_block_size;   //本包长度(不包括命令头)
        buffer[31] = (byte) ((order >> 8) & 0xff);    //命令序号
        buffer[32] = (byte) (order & 0xff);
        if (mac == null || mac.isEmpty()) {
            Arrays.fill(buffer, 33, (33 + 10), (byte) 0);
        } else {
            byte[] tmp;
            tmp = Arrays.copyOf(mac.getBytes(), Math.min(mac.length(), 10));
            System.arraycopy(tmp, 0, buffer, 33, tmp.length);
        }


        buffer[43] = 0x01;//增加是否自动启动刺绣0x00不启动，0x01启动
        Arrays.fill(buffer, 44, 44 + 6, (byte) 0);
        order++;
    }

    //填写文件信息(buffer缓冲区/size文件总长度/file文件名/len当前包的数据长度)
    public static void msgFileInfo(byte buffer[], int size, String file, short len) {
        //  USHORT m_protocol_type;//协议类型
        //  USHORT m_commi_bianhao;//数据编号
        buffer[4] = (byte) ((size >> 24) & 0xff);    //传输文件的长度
        buffer[5] = (byte) ((size >> 16) & 0xff);
        buffer[6] = (byte) ((size >> 8) & 0xff);
        buffer[7] = (byte) (size & 0xff);
        // 	ULONG  m_block_lvl;    //包号
        // 	BYTE   m_trans_mark;   //传输完成标志未完成/否则完成
        if (!file.isEmpty()) {
            System.arraycopy(file.getBytes(), 0, buffer, 13, 11);
        } else {
            Arrays.fill(buffer, 13, 13 + 11, (byte) 0);
        }
        // 	USHORT m_file_num;     //文件个数
        buffer[29] = (byte) ((len >> 8) & 0xff);   //本包长度(不包括命令头)
        buffer[30] = (byte) (len & 0xff);
        // 	USHORT m_order_num;    //命令序号
        // 	BYTE   m_mac_name[10]; //设备名称
        // 	BYTE   m_reserve[7];   //保留
    }
}
